/**
 * 产品修改js
 */
var comValidform = null;
var returnStatus = false;
var question = {
    curPage: 1,
    pageSize: 10,
    dataCache: null,

    questionUE: null,

    startEvent: function () {
        $("#recordTab ul").idTabs();

        //comValidform = $(".com-addForm").Validform({
        //    datatype: {
        //        "m0-2": /^[0-9]+(.[0-9]{1,2})?$/
        //    },
        //    tiptype: 2,
        //    tipSweep: true,
        //    beforeSubmit: function (curform) {
        //        var url = 'mgr/questionCtrl/updatequestionInfo';
        //        var params = jsUtils.serializeJson($('.com-addForm'));
        //
        //        var questionText = question.questionUE.getContent();
        //        params.question = questionText;
        //
        //        var cb = function (data) {
        //            alert(data.msg);
        //            // 成功返回
        //            if (data.resultCode == 200) {
        //                // 关闭窗口并刷新数据
        //                art.dialog.close();
        //                returnStatus = true;
        //            }
        //        };
        //        sendApiNoLoadSync(url, params, cb, 'POST');
        //        return false;
        //    }
        //});
    },

    // 设置分页
    setPageChange: function (delCurPage) {
        var url = 'mgr/questionCtrl/list';
        var name = $.trim($('#searchModForm input[name="name"]').val());
        var params = {
            'curPage': 1,
            'pageSize': 99999,
            'name': name,
            'id': 1
        };

        var cb = function (data) {
            if (data.resultCode == 200) {
                var totalCount = data.size;
                var pageTotal = (totalCount / question.pageSize > 0) ? Math
                    .ceil(totalCount / question.pageSize) : 1;
                pageHelper.init(pageTotal, totalCount, question.search,
                    delCurPage); // 翻页事件
            }
        };
        sendApiNoLoad(url, params, cb, 'POST');
    },

    // 查询系统用户
    search: function (pageIdx) {
        var url = 'mgr/questionCtrl/list';
        if (typeof (pageIdx) != "undefined") {
            question.curPage = pageIdx;
        }
        var name = $.trim($('#searchModForm input[name="name"]').val());
        var params = {
            'curPage': question.curPage,
            'pageSize': question.pageSize,
            'name': name
        };
        var cb = function (data) {
            $('#tableList tbody').empty();
            var tr = '';
            if (data.resultCode == 200) {
                if (data.result != null && data.result.length > 0) {
                    for (var i = 0; i < data.result.length; i++) {
                        var singleObj = data.result[i];
                        var orderCreatedOn = getSmpFormatDateByLong(
                            singleObj.cts, true);
                        localStorage.setItem(singleObj.id + "_question_Key",
                            JSON.stringify(singleObj));

                        tr += '<tr><td><input type="hidden" name="id" value="'
                            + singleObj.id
                            + '"/>'
                            + singleObj.id
                            + '</td><td>'
                            + singleObj.seqNum
                            + '</td><td>'
                            + singleObj.groupId
                            + '</td><td>'
                            + singleObj.question
                            + '</td><td>'
                            + orderCreatedOn
                            + '</td><td>'
                            + '<a type="button" name="question_99" class="btn btn-primary btn-xs lookData" style="margin-left: 5px;">'
                            + '<i class="fa fa-reorder"></i> 查看 </a>'
                                //+ '<a type="button" name="question_96" class="btn btn-success btn-xs updateData" style="margin-left: 5px;">'
                                //+ '<i class="fa fa-edit"></i>修改</a>'
                            + '</td></tr>';
                    }
                } else {
                    tr = '<tr><td colspan="7" style="text-align: center;color: red;">暂无数据</td></tr>';
                }
            } else {
                tr = '<tr><td colspan="7" style="text-align: center;color: red;">暂无数据</td></tr>';
            }
            $('#tableList tbody').html(tr);
            question.bindEvent();
            jsUtils.controlFunctionBtn();
        };
        sendApiNoLoad(url, params, cb, 'POST');
    },

    bindEvent: function () {
        // 查询
        $('#searchData').click(function () {
            question.curPage = 1;
            question.setPageChange(null);
            question.search();
        });
        //$('.updateData').unbind();
        //$('.updateData')
        //    .on(
        //        'click',
        //        function () {
        //            var thisObj = $(this);
        //
        //            var id = thisObj.parent().parent().find(
        //                'input[name="id"]').eq(0).val();
        //            art.dialog.data('updId', id);
        //
        //            var path = APP_ADDRESS
        //                + 'mgr/other/kyc/questionUpdate.jsp';
        //            art.dialog
        //                .open(
        //                    path,
        //                    {
        //                        title: '修改协议',
        //                        width: 900,
        //                        height: 500,
        //                        ok: function () {
        //                            var returnStatus = this.iframe.contentWindow
        //                                .submitForm();
        //                            if (returnStatus) {
        //                                question
        //                                    .setPageChange(question.curPage);
        //                                question
        //                                    .search(question.curPage);
        //                            }
        //                            return false;
        //                        },
        //                        cancel: true
        //                    });
        //        });

        $('#addData').unbind();
        $('#addData').on('click', function () {
            var path = APP_ADDRESS + 'mgr/other/kyc/questionAdd.jsp';
            art.dialog.open(path, {
                title: '添加问题',
                width: 1000,
                height: 800,
                ok: function () {
                    var returnStatus = this.iframe.contentWindow.submitForm();
                    if (returnStatus) {
                        question.setPageChange(null);
                        question.search();
                    }
                    return false;
                },
                cancel: true
            });
        });


        //$('#test').on('click', function () {
        //    var url = 'common/orderRoute/createFYJZHOrder';
        //
        //
        //    var params = {
        //        userId: "75284640276359",
        //        productId: "84979413151241",
        //        userCardId: "84202523098528",
        //        buyAmount: 20000
        //    };
        //
        //    var cb = function (data) {
        //        // 成功返回
        //        if (data.resultCode == 200) {
        //            var totalCount = data.size;
        //            var pageTotal = (totalCount / question.pageSize > 0) ? Math
        //                .ceil(totalCount / question.pageSize) : 1;
        //            pageHelper.init(pageTotal, totalCount, question.search,
        //                delCurPage); // 翻页事件
        //        }
        //    };
        //    sendApiNoLoadSync(url, params, cb, 'POST');
        //    return false;
        //
        //});

        $('.lookData').on(
            'click',
            function () {
                var thisObj = $(this);
                var id = thisObj.parent().parent().find('input[name="id"]')
                    .eq(0).val();
                art.dialog.data('lokId', id);
                var path = APP_ADDRESS + 'mgr/other/kyc/questionLook.jsp';
                art.dialog.open(path, {

                    title: '查看协议',
                    width: 800,
                    height: 800,
                    cancel: true
                });
            });

    },
    showInfoById: function () {
        var updId = art.dialog.data('updId'); // 修改id
        var url = 'mgr/questionCtrl/getAgrrementInfoById';
        var params = {
            'id': updId
        };
        var cb = function (data) {
            if (data.resultCode == 200) {
                var ret = data.result;

                if (ret.questionTxt != null) {
                    question.questionUE.ready(function () {
                        question.questionUE.setContent(ret.riskctrlTxt);
                    });
                }
            }
        };
        sendApiNoLoad(url, params, cb, 'POST');
    },

    init: function () {
        question.setPageChange(null);
        question.search();
        question.startEvent();
        question.bindEvent();
    }
}
$(document).ready(function () {
    question.init();
});
function submitForm() {
    comValidform.submitForm();
    return returnStatus;
}